"""
高德地图api
"""
import requests
import json

from .ApiBasic import ApiBasic


class GaodeApi(ApiBasic):
    key = "efe4e9291a4a665ff691c55e3a3b871d"

    def __init__(self):
        self.url = "https://restapi.amap.com/v3"
        self.headers = {
            'User-Agent': 'Apifox/1.0.0 (https://apifox.com)',
            'Accept': '*/*',
            'Host': 'restapi.amap.com',
            'Connection': 'keep-alive'
        }

    def regeo(self, gps_location):
        """
        逆地理编码
        """
        url = self.url + "/geocode/regeo?key={}&location={}".format(self.key, gps_location)

        payload = {}

        response = requests.request("GET", url, headers=self.headers, data=payload)
        return self.make_response(response)

    def make_response(self, response, message="请求失败"):
        if response.status_code == 200:
            return json.loads(response.text)
        else:
            raise ValueError(message)
